#include "world_manager.h"
#include <iostream>

namespace world {

WorldManager& WorldManager::instance()
{
    static WorldManager instance;
    return instance;
}

WorldManager::WorldManager()
{
    std::cout << "WorldManager initialized" << std::endl;
}

WorldManager::~WorldManager()
{
    std::cout << "WorldManager shutdown" << std::endl;
}

bool WorldManager::init()
{
    // 初始化世界管理器
    std::cout << "Initializing WorldManager..." << std::endl;
    
    // 加载场景数据
    // ...
    
    return true;
}

void WorldManager::update()
{
    // 更新所有场景
    for (auto& pair : scenes_) {
        pair.second->update();
    }
}

void WorldManager::shutdown()
{
    // 清理资源
    scenes_.clear();
    entities_.clear();
}

bool WorldManager::add_scene(std::shared_ptr<Scene> scene)
{
    if (!scene)
        return false;
    
    scenes_[scene->get_id()] = scene;
    return true;
}

std::shared_ptr<Scene> WorldManager::get_scene(int scene_id) const
{
    auto it = scenes_.find(scene_id);
    if (it != scenes_.end())
        return it->second;
    
    return nullptr;
}

void WorldManager::remove_scene(int scene_id)
{
    scenes_.erase(scene_id);
}

bool WorldManager::add_entity_to_scene(std::shared_ptr<Entity> entity, int scene_id)
{
    if (!entity)
        return false;
    
    auto scene = get_scene(scene_id);
    if (!scene)
        return false;
    
    // 添加实体到场景
    scene->add_entity(entity);
    
    // 保存实体引用
    entities_[entity->get_id()] = entity;
    
    return true;
}

void WorldManager::remove_entity_from_scene(int entity_id, int scene_id)
{
    auto scene = get_scene(scene_id);
    if (scene) {
        scene->remove_entity(entity_id);
    }
    
    entities_.erase(entity_id);
}

std::shared_ptr<Entity> WorldManager::get_entity(int entity_id) const
{
    auto it = entities_.find(entity_id);
    if (it != entities_.end())
        return it->second;
    
    return nullptr;
}

} // namespace world    